﻿Imports MeoTom.Core

Partial Public Class Login
    Inherits UI.Page
    Public u As New BLL.Users()
    Public util As New Utilities.Common()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("loggedout") = "true" Then
            lblError.Visible = True
            lblError.Attributes.Remove("class")
            lblError.Attributes.Add("class", "lblDisplayInfo")
            lblError.InnerHtml = "* You are now logged out."
        End If
    End Sub

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
        Dim strUrlReturn As String = ""
        If ValidateLogin() = True Then
            If Request.QueryString("ReURL") = vbNullString Then
                strUrlReturn = "default.aspx"
            Else
                strUrlReturn = Request.QueryString("ReURL")
            End If
            Response.Redirect(strUrlReturn, False)
        Else
            lblError.Visible = True
            lblError.Attributes.Remove("class")
            lblError.Attributes.Add("class", "lblDisplayError")
            lblError.InnerHtml = "* ERROR: Incorrect password. <a href=""LostPassword.aspx"" title=""Lost your password"">Lost your password?</a>."
        End If
    End Sub

    Public Function ValidateLogin() As Boolean
        Try
            Dim b As Boolean = False
            u.UserName = util.NoHTML(txtUserName.Text)
            u.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(util.NoHTML(txtPassword.Text), "MD5")
            If u.Login() Then
                'If cboRemember.Checked Then
                util.SetCookie("UserID", u.ID.ToString(), DateTime.Now.AddDays(30))
                util.SetCookie("UserName", u.UserName, DateTime.Now.AddDays(30))
                util.SetCookie("Email", u.Email, DateTime.Now.AddDays(30))
                'Else
                '    Session.Timeout = 36000
                '    util.SetSession("UserID", u.ID.ToString())
                '    util.SetSession("UserName", u.UserName)
                '    util.SetSession("Email", u.Email)
                'End If
                b = True
            Else
                b = False
            End If
            Return b
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class